package com.shj.em.biz.sys.controller;

import com.shj.em.biz.sys.entity.Cd;
import com.shj.em.biz.sys.service.CdService;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.sys.entity.CdType;
import com.shj.em.biz.sys.service.CdTypeService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;


/**
 * <p>
 * 代码项 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-04
 */
@RestController
@Slf4j
@RequestMapping("/sys/cdtype")
public class CdTypeController {

    @Autowired
    CdTypeService cdtypeService;

    @Autowired
    CdService cdService;

    @RequestMapping("/list")
    public PageVO<CdType> list(@RequestBody Map<String, Object> params){
        Page<CdType> page = cdtypeService.queryCdTypePage(params);
        PageVO<CdType> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @GetMapping("/findById/{id}")
    public ResMsg findCdTypeById(@PathVariable long id){
        CdType cdType = cdtypeService.selectById(id);
        List<Cd> items = cdService.queryCdsByType(cdType.getType());
        cdType.setItems(items);
        return ResMsg.ok(cdType);
    }


    @Log("创建代码类型")
    @ResponseBody
    @PostMapping("/add")
    public ResMsg add(CdType cdtype){
        try{
            boolean exist = cdtypeService.isCdTypeExist(cdtype.getType(), null);
            if(exist){
                return ResMsg.error("代码类型已经存在");
            }
            cdtypeService.insert(cdtype);
            return ResMsg.ok(new CdType());
        }catch(Exception e){
            log.error("创建代码项失败", e);
            return ResMsg.error("创建代码项失败");
        }

    }

    @Log("更新代码类型")
    @ResponseBody
    @PostMapping("/edit")
    public ResMsg edit(CdType cdtype){
        try{
            boolean exist = cdtypeService.isCdTypeExist(cdtype.getType(), null);
            if(exist){
                return ResMsg.error("代码类型已经存在");
            }
            cdtypeService.updateById(cdtype);
            return ResMsg.ok();
        }catch(Exception e){
            log.error("更新代码项失败", e);
            return ResMsg.error("更新代码项失败");
        }

    }

    @Log("批量删除代码类型")
    @PostMapping( "/remove")
    @ResponseBody
    public ResMsg remove(@RequestParam("id") Long id){
        try{
            cdtypeService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
    }

    /**
     * 批量删除
     */
    @PostMapping( "/batchRemove")
    @ResponseBody
    public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        cdtypeService.deleteBatchIds(Arrays.asList(ids));
        return ResMsg.ok();
    }

}

